package spe.ui.editor.part;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;

import spe.model.notation.SPEMDiagram;
import spe.model.spem.NodeElement;
import spe.model.spem.SPEMElement;
import spe.ui.editor.policy.DiagramXYLayoutPolicy;
import spe.ui.editor.policy.SPEMGraphicalNodeEditPolicy;

public class GraphEditPart extends AbstractGraphicalEditPart implements PropertyChangeListener
{
	public GraphEditPart(SPEMDiagram mdl)
	{
		setModel(mdl);
	}

	@Override
	protected IFigure createFigure() 
	{
		FreeformLayer layer = new FreeformLayer();
	    layer.setLayoutManager(new FreeformLayout());
	    layer.setBorder(new LineBorder(1));
	    return layer;
	}

	@Override
	protected void createEditPolicies() 
	{
		//installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramXYLayoutPolicy());
		//installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SPEMGraphicalNodeEditPolicy());
	}
	
	@Override
	protected List<NodeElement> getModelChildren()
	{
		List<NodeElement> lst = ((SPEMDiagram)getModel()).getElements();
		//System.out.println(String.format("Num of Elements: %d", lst.size()));
		return lst;
	}
	
	@Override
	public void activate()
	{
		super.activate();
		((SPEMDiagram)getModel()).addPropertyChangeListener(this);
	}
	
	@Override
	public void deactivate()
	{
		((SPEMDiagram)getModel()).removePropertyChangeListener(this);
		super.deactivate();
	}

	@Override
	public void propertyChange(PropertyChangeEvent event)
	{
		// TODO Auto-generated method stub
		//System.out.println(String.format("[GraphEditPart] value: %s", event.getNewValue()));
		refreshChildren();
	}
}
